﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QLDBHYT.DAO;
using QLDBHYT.VO;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace QLDBHYT.BUS
{
    class P1hBUS
    {
        private P1hDAO p1hDAO;
        public P1hBUS()
        {
            p1hDAO = new P1hDAO();
        }
        public P1hVO GetMathe(string mathe)
        {

            P1hVO p1dVO = new P1hVO();
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.SearhMathe(mathe);
            foreach (DataRow dr in dataTable.Rows)
            {
                p1dVO.Hoten = dr["ho_ten"].ToString();
                p1dVO.Gioitinh = Convert.ToInt16(dr["gioi_tinh"]);
                p1dVO.Ngaysinh = Convert.ToInt16(dr["ngay_sinh"]);
                p1dVO.Thangsinh = Convert.ToInt16(dr["thang_sinh"]);
                p1dVO.Namsinh = Convert.ToInt16(dr["nam_sinh"]);
                p1dVO.Diachi  = dr["dia_chi"].ToString();
                p1dVO.Tinhkcb = Convert.ToString(dr["tinh_kcb"]);
                p1dVO.Makcb = dr["ma_kcb"].ToString();
                p1dVO.Noikham  = dr["noi_kham"].ToString();
                p1dVO.Gttu = dr["gtri_tu"].ToString();
                p1dVO.Gtden = dr["gtri_den"].ToString();
                p1dVO.Benhvien = dr["noi_kham"].ToString();
              
            }
            return p1dVO;
        }
        public bool CheckMaTheTheoNgay(string ngay, string mathe,string sophieu)
        {
            P1hVO p1dVO = new P1hVO();
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.SearchMaTheTheoNgay(ngay, mathe,sophieu);
            if (dataTable.Rows.Count >= 1)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        public List<khoakhamVO> GetKhoaKham()
        {

            List<khoakhamVO> lstP1hVO = new List<khoakhamVO>();
            khoakhamVO P1hVO;
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.SearchKhoa();
            foreach (DataRow dr in dataTable.Rows)
            {
                P1hVO = new khoakhamVO();
                P1hVO.Makhoa = dr["Ma_khoa"].ToString();
                P1hVO.Tenkhoa = dr["Ten_khoa"].ToString();
                lstP1hVO.Add(P1hVO);
            }
            return lstP1hVO;
        }
        public List<DoituongVO> GetDoiTuong()
        {

            List<DoituongVO> lstDoituongVO = new List<DoituongVO>();
            DoituongVO doituongVO;
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.SearchDoiTuong();
            foreach (DataRow dr in dataTable.Rows)
            {
                doituongVO = new DoituongVO();
                doituongVO.Madt = dr["ma_dt"].ToString();
                doituongVO.Loaitt = dr["loai_tt"].ToString();
                doituongVO.Vanchuyen = dr["vanchuyen"].ToString();
                lstDoituongVO.Add(doituongVO);
            }
            return lstDoituongVO;
        }
        public List<ThuocVO> GetTenHieu(int thang,int nam)
        {

            List<ThuocVO> lstThuocVO = new List<ThuocVO>();
            ThuocVO thuocVO;
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.SearchTenHieu(thang,nam);
            foreach (DataRow dr in dataTable.Rows)
            {
                if (!( Convert.ToInt32(dr["slcuoiky"]) == 0 && (dr["stt_nh"].ToString().Trim() == "01"
                    || dr["stt_nh"].ToString().Trim() == "03") ))
                {
                    thuocVO = new ThuocVO();
                    thuocVO.Sttnh  = dr["stt_nh"].ToString().Trim();
                    thuocVO.Mahieu = dr["Ma_hieu"].ToString().Trim();
                    thuocVO.Tenhieu = dr["ten_hieu"].ToString().Trim();
                    thuocVO.Dvt = dr["dvt"].ToString();
                    thuocVO.Gia  = Convert.ToDecimal(dr["gia"]);
                    thuocVO.Soluong = Convert.ToInt32(dr["slcuoiky"]);
                    thuocVO.Giabhxh = Convert.ToDecimal(dr["gia_bhxh"]);
                    thuocVO.Ktc = dr["ktc"].ToString();
                    lstThuocVO.Add(thuocVO);
                }
            }
            return lstThuocVO;
        }
        public P1hVO InsertRecord(P1hVO p1hVO, List<P1duVO> lstP1duVO, List<P1dVO> lstP1dVO)
        {
            return p1hDAO.InsertRecord(p1hVO, lstP1duVO, lstP1dVO);
        }
        public P1hVO UpdateRecord(P1hVO p1hVO, List<P1duVO> lstP1duVO, List<P1dVO> lstP1dVO)
        {
            return p1hDAO.UpdateRecord(p1hVO, lstP1duVO, lstP1dVO);
        }
        public bool DeleteRecord(string ctid)
        {
            return p1hDAO.DeleteRecord(ctid);
        }
        public List<P1hVO> GetListP1H(string table,string condition)
        {
            List<P1hVO> lstP1H = new List<P1hVO>();
            P1hVO p1hVO;
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.Search(table,condition);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p1hVO = new  P1hVO();
                GetP1H(p1hVO, dr);
                lstP1H.Add(p1hVO);

            }
            return lstP1H;
        }
        private void GetP1H(P1hVO p1hVO, DataRow dr)
        {
            p1hVO.Ctid = dr["ct_id"].ToString();
            p1hVO.Loai = dr["loai"].ToString();
            p1hVO.Matinh = dr["matinh"].ToString();
            p1hVO.Sophieu = dr["sophieu"].ToString();
            p1hVO.Benhvien = dr["benh_vien"].ToString();
            p1hVO.Makhoa = Convert.ToString(dr["ma_khoa"]);
            p1hVO.Tenkhoa = dr["ten_khoa"].ToString();
            p1hVO.Mathe = dr["mathe"].ToString();
            p1hVO.Madt = dr["ma_dt"].ToString();
            p1hVO.Loaitt = Convert.ToString(dr["loai_tt"]);
            p1hVO.Vanchuyen = Convert.ToString(dr["vanchuyen"]);
            p1hVO.Loaitt1 = Convert.ToDecimal(dr["loai_tt1"]);
            p1hVO.Hoten = Convert.ToString(dr["ho_ten"]);
            p1hVO.Gioitinh = Convert.ToInt16(dr["gioi_tinh"]);
            p1hVO.Ngaysinh = Convert.ToInt32(dr["ngay_sinh"]);
            p1hVO.Thangsinh = Convert.ToInt32(dr["thang_sinh"]);
            p1hVO.Namsinh = Convert.ToInt32(dr["nam_sinh"]);
            p1hVO.Diachi = Convert.ToString(dr["dia_chi"]);
            p1hVO.Tinhkcb  = Convert.ToString(dr["tinh_kcb"]);
            p1hVO.Makcb = Convert.ToString(dr["ma_kcb"]);
            p1hVO.Noikham = Convert.ToString(dr["noi_kham"]);
            p1hVO.Ngaykham = Convert.ToString(dr["ngay_kham"]);
            p1hVO.Gttu = Convert.ToString(dr["gtri_tu"]);
            p1hVO.Gtden = Convert.ToString(dr["gtri_den"]);
            p1hVO.Ngaytt = Convert.ToString(dr["ngay_tt"]);
            p1hVO.Maicd = Convert.ToString(dr["ma_icd"]);
            p1hVO.Tenbenh = Convert.ToString(dr["ten_benh"]);
            p1hVO.Benhan = Convert.ToString(dr["benhan"]);
            p1hVO.Mabhxh = Convert.ToString(dr["ma_bhxh"]);
            p1hVO.Lenhdx = Convert.ToString(dr["lenh_dx"]);
            p1hVO.Noiden = Convert.ToString(dr["noi_den"]);
            p1hVO.Ngay1 = Convert.ToString(dr["ngay_g1"]);
            p1hVO.Ngay2 = Convert.ToString(dr["ngay_g2"]);
            p1hVO.Ngay3 = Convert.ToString(dr["ngay_g3"]);
            p1hVO.Ngaylv = Convert.ToString(dr["ngay_lv"]);
            p1hVO.Usernam = Convert.ToString(dr["user_name"]);
            p1hVO.Excel = Convert.ToInt32(dr["excel"]);
            p1hVO.Doituong = Convert.ToInt32(dr["doituong"]);
            p1hVO.Dieutri = Convert.ToString(dr["dieu_tri"]);
        }
        public List<P1duVO> GetListP1DU(string table, string ctid)
        {
            List<P1duVO> lstP1du = new List<P1duVO>();
            P1duVO p1duVO;
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.SearchP1DU(table, ctid);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p1duVO = new P1duVO();
                p1duVO.Ctid = ctid;
                p1duVO.Ctid2 = dr["ct_id2"].ToString();
                p1duVO.Sttnh = dr["stt_nh"].ToString();
                p1duVO.Tongtien = Convert.ToDecimal(dr["tong_tien"]);
                p1duVO.Tienbn = Convert.ToDecimal(dr["tien_bn"]);
                lstP1du.Add(p1duVO);

            }
            return lstP1du;
        }
        public List<P1dVO> GetListP1D(string table, string ctid)
        {
            List<P1dVO> lstP1d = new List<P1dVO>();
            P1dVO p1dVO;
            DataTable dataTable = new DataTable();
            dataTable = p1hDAO.SearchP1D(table, ctid);
            if (dataTable == null) { return null; }
            foreach (DataRow dr in dataTable.Rows)
            {
                p1dVO = new P1dVO();
                p1dVO.Ctid = ctid;
                p1dVO.Ctid0 = dr["ct_id0"].ToString();
                p1dVO.Sttnh = dr["stt_nh"].ToString();
                p1dVO.Mahieu = dr["Ma_hieu"].ToString();
                p1dVO.Tenhieu = dr["ten_hieu"].ToString();
                p1dVO.Tyle = 100;
                p1dVO.Ktc = dr["ktc"].ToString();
                p1dVO.Sl = Convert.ToInt16(dr["sl"]);
                p1dVO.Gia = Convert.ToDecimal(dr["gia"]);
                p1dVO.Giabhxh = Convert.ToDecimal(dr["gia_bhxh"]);
                p1dVO.Tongtien = Convert.ToDecimal(dr["tong_tien"]);
                p1dVO.Tienbhxh = Convert.ToDecimal(dr["tien_bhxh"]);
                p1dVO.Tienchenhlech = 0;//Convert.ToInt16(fa.Rows[i]["Tienchenhlech"]);
                p1dVO.Tien_bn = Convert.ToDecimal(dr["tien_bn"]);
                p1dVO.Dvt = dr["dvt"].ToString();
                p1dVO.Loai = 1;
                p1dVO.Bacsy = "";
                p1dVO.Khoa = "";
                p1dVO.Chon = 0;
                p1dVO.Flag = 0;
                lstP1d.Add(p1dVO);

            }
            return lstP1d;
        }
        public DataTable GetBV(string BV)
        {
            return p1hDAO.SearchBV(BV);
        }

    }
}
